/*
 * @Copyright: 2016 www.yyfax.com Inc. All rights reserved.
 */
package com.yyfax.pay.order.data.dao.impl;

import java.sql.SQLException;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import com.yyfax.framework.dao.impl.BaseDaoImpl;
import com.yyfax.pay.order.data.dao.NotifyTaskDao;
import com.yyfax.pay.order.model.po.NotifyTaskPO;

/**
 * 异步通知任务,用于异步通知业务模块持久化实现
 *
 * @author buyi
 * @date 2017年04月12日 11:09:15
 * @since 1.0.0
 */
@Repository
public class NotifyTaskDaoImpl extends BaseDaoImpl<NotifyTaskPO, Integer> implements NotifyTaskDao {
	/** 表名，该值要和notify_task.xml的属性"tableName"保持一致 */
	private static final String TABLE_NAME = "notify_task";

	/** 方法名，该值要和notify_task.xml的属性"id"保持一致 */
	private final String SQL_INSERT = "insert";

	/** 方法名，该值要和query_task.xml的属性"id"保持一致 */
	// private final String SQL_UPDATE = "update";

	private final String SQL_UPDATE_BY_MERCHANT_ORDERID = "updateByMerchantAndOrderId";

	/** 方法名，该值该值要和notify_task.xml的属性"id"保持一致 */
	private final String SQL_SELECT_BY_ID = "selectById";

	public NotifyTaskDaoImpl() {
		super(TABLE_NAME);
	}

	@Override
	public int insert(NotifyTaskPO po) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_INSERT);
		BeanPropertySqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(po);
		return namedParameterJdbcTemplate.insertForPK(sql, sqlParameterSource);
	}

	// @Override
	// public int update(NotifyTaskPO po) {
	// String sql = sqlFactory.getSQL(TABLE_NAME, SQL_UPDATE);
	// BeanPropertySqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(po);
	// return namedParameterJdbcTemplate.update(sql, sqlParameterSource);
	// }

	@Override
	public NotifyTaskPO selectByIdAndMerchantCode(int id, String merchantCode) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_ID);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("id", id);
		return namedParameterJdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper<NotifyTaskPO>(poClass));
	}

	@Override
	public int updateByMerchantAndOrderId(NotifyTaskPO po) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_UPDATE_BY_MERCHANT_ORDERID);
		BeanPropertySqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(po);
		return namedParameterJdbcTemplate.update(sql, sqlParameterSource);
	}

	@Override
	public int updateState(NotifyTaskPO po) {
		String sql = sqlFactory.getSQL(TABLE_NAME, "updateState");
		BeanPropertySqlParameterSource sqlParameterSource = new BeanPropertySqlParameterSource(po);
		return namedParameterJdbcTemplate.update(sql, sqlParameterSource);
	}

}
